#coding:utf-8

from google.appengine.ext import db
from lib.models import Course
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import os
from google.appengine.api import memcache
from google.appengine.api import users

class MainPage(webapp.RequestHandler):
    def get(self):

        page = memcache.get("indexpage")

        user = users.get_current_user()
        if user:
            if users.is_current_user_admin():
                page=None


        if page is None:

            sept = Course.all().filter("has_sept =", True)
            febr = Course.all().filter("has_febr =", True)
            june = Course.all().filter("has_june =", True)

            template_values = {
                    "title":"Τελευταία Μαθήματα",
                    "sept":sept,
                    "febr":febr,
                    "june":june,
                    }

            path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
            page = template.render(path, template_values)
            memcache.add("indexpage", page, 120)

        self.response.out.write(page)
application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
